/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include <fstream>
#include <sstream>

#include "input/KeyConverter.h"
#include "input/Binder.h"
#include "shared/StringUtil.h"

#include "ClientConfig.h"
#include "Kernel.h"

extern Kernel* g_kernel;


ClientConfig::ClientConfig(Cvars* cvar, Binder* binds)
{
   save_f.Set( this );

   m_Cvars = cvar;
   m_Binds = binds;
   g_kernel->GetConsole()->AddCommand( &save_f, "save_cfg");
}

/**
==================
Register player key binds
==================
*/
void ClientConfig::Register( const std::string &cmd, int key )
{
    KeyConverter keyCon;
    m_Cvars->RegisterCvar( cmd, keyCon.ToString( key ), key, CVAR_SAVE|CVAR_BIND );
}

/**
======================
Save the users configuration
======================
*/
int ClientConfig::Save(const std::string &fileName, const std::string &Header )
{
    KeyConverter    key;
    std::ofstream   stream;

    stream.open( fileName.c_str() );

    stream << "%% ----------------- " + fileName + " ------------------ %%\n";
    stream << Header;
    
    /*
     * Also print anything that may have been binded
     */
    
    std::map<int, std::string >::iterator iter = m_Binds->m_bindings.begin();
    for(; iter != m_Binds->m_bindings.end();iter++ )
    {
        stream << "bind " +  key.ToString( iter->first) + " " + iter->second + ";\n";
    }
    /**
     * Print each cvar
     */
    type_CvarList::const_iterator it = m_Cvars->GetCvars().begin();
    for( ; it != m_Cvars->GetCvars().end(); ++it )
    {
        if ( !(it->second->flags & CVAR_BIND) ) 
        {
            if ( it->second->svalue == "" ) {
                StringUtil util;             
                stream << "set " + it->second->name + " " + util.TToString<float>( it->second->vector[0] ) + " " +
                                                            util.TToString<float>( it->second->vector[1] ) + " " +
                                                            util.TToString<float>( it->second->vector[2] ) + ";\n";
            }
            else stream << "set " + it->second->name + " " + it->second->svalue + ";\n";
        }
        
    }
    g_kernel->LPrintf( ("Saving " + fileName + "...").c_str() );
    
    stream.close();
    return 0;
}

ClientConfig::~ClientConfig(void)
{
    g_kernel->GetConsole()->RemoveCommand( "save_cfg" );
}


/**
==================================
Save Command
==================================
*/
void SaveCFG_f::Exec(std::string &s)
{ 
    if ( m_config )
    {
        StringUtil util(s);
        if ( util.Size() > 1 ) {
            std::string fileName = util.GetNext();        
            m_config->Save( fileName, s.substr( fileName.size(), util.GetNext().size() ) );
        
        } else
            m_config->Save(s,"");
    }
};